﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ELMWCFServiceClass.ELMWCF;
using System.ComponentModel;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Modularity;
using System.Collections.ObjectModel;
using ELMApplication.Common.Const;
using System.Linq;
using ELMApplication.Common.UC;
using ELMApplication.Common.Util;

namespace UserLoginModule.Views.AddUserView
{
    public class AddUserPrentationModel:IAddUserPrentationModel
    {

         private ELMServiceClient _domaincontext;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;

        public AddUserPrentationModel(IAddUserView view, ELMServiceClient domaincontext, IEventAggregator eventAggregator, IRegionManager regionManager, IModuleManager moduleManager)
        {
            this._domaincontext = domaincontext;            
            this._eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.moduleManager = moduleManager;
            this.View = view;           
            view.SetModel(this);
            this.ELMListData.DataLoadCompleted += new EventHandler(ELMListData_DataLoadCompleted);
            ELMListData.EditEvent += new EventHandler<ELMApplication.Common.Events.ELMListEventArgs>(hmList_EditEvent);
            ELMListData.DelEvent += new EventHandler<ELMApplication.Common.Events.ELMListEventArgs>(hmList_DelEvent);
            ELMListData.NewEvent += new EventHandler<ELMApplication.Common.Events.ELMListEventArgs>(ELMListData_NewEvent);
            //initialData();
        }

        void ELMListData_NewEvent(object sender, ELMApplication.Common.Events.ELMListEventArgs e)
        {
            AddingHuman = new Human();
            ShowDetial(PrivegeTypes.New, "");
        }
        void hmList_DelEvent(object sender, ELMApplication.Common.Events.ELMListEventArgs e)
        {
            MessageBoxResult res = MessageBox.Show("确定删除？", "", MessageBoxButton.OKCancel);
            if (res == MessageBoxResult.OK)
            {
                delUser(e.DataKey);
            }
        }

        void hmList_EditEvent(object sender, ELMApplication.Common.Events.ELMListEventArgs e)
        {
            // model.SetAddingHuman();
            ShowDetial(PrivegeTypes.Edit, e.DataKey);
        }

        void ELMListData_DataLoadCompleted(object sender, EventArgs e)
        {
            BusyMessage.Close();
        }

        public void initialData()
        {
            BusyMessage.Show("数据读取中...");
            _domaincontext = new ELMServiceClient();
            //_domaincontext.GetAllHumanAsync();
            //_domaincontext.GetAllHumanCompleted += (sender, data) =>
            //    {
            //        if (data.Error == null)
            //        {
            //            AllHuman = data.Result;
            //        }

            //    };
            this.ELMListData.LoadHuman(new Guid("293f62fc-e911-4b32-b8b3-b55773efa850"));
            AddingHuman = new Human();
            _domaincontext.GetAllDutyAsync();
            _domaincontext.GetAllDutyCompleted += (sender, data) =>
            {
                if (data.Error == null)
                {
                    AllDuty = data.Result;
                }
            };
            _domaincontext.GetContentTypeByIdAsync(new Guid("293f62fc-e911-4b32-b8b3-b55773efa850"));
            _domaincontext.GetContentTypeByIdCompleted += (sender, data) =>
            {
                if (data.Error == null)
                {
                    ExtendInfo = data.Result;

                }
                else
                {
                    throw data.Error;
                }

            };
           
        }
        

       

        #region IAddUserPrentationModel Members

        public IAddUserView View
        {
            get;
            set;
        }

        public void AddUser()
        {
            if (!string.IsNullOrEmpty(AddingHuman.HumanId) && !string.IsNullOrEmpty(AddingHuman.HumanName) && !string.IsNullOrEmpty(AddingHuman.Password))
            {
                _domaincontext.AddNewUserAsync(AddingHuman);
                _domaincontext.AddNewUserCompleted += (sender, data) =>
                    {
                        if (data.Error == null && data.Result)
                        {
                            MessageBox.Show("添加成功");
                            AddingHuman = new Human();
                            initialData();
                        }
                        else
                        {
                            MessageBox.Show("添加失败，请检查帐号和其他数据");

                        }
                    };
            }
            else
            {
                MessageBox.Show("数据不完整，请补充！");
            }
            BusyMessage.Close();
        }
        private Human _addingHuman;
        public Human AddingHuman
        {
            get
            {
                return _addingHuman;
            }
            set
            {
                _addingHuman = value;
                NotifyPropertyChanged("AddingHuman");
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region IAddUserPrentationModel Members

        private ObservableCollection<t_Duty> _allDuty;
        public ObservableCollection<t_Duty> AllDuty
        {
            get
            {
                return _allDuty;
            }
            set
            {
                _allDuty=value;
                NotifyPropertyChanged("AllDuty");
            }
        }

        #endregion

        #region IAddUserPrentationModel Members

        private t_Duty _selectedDuty;
        public t_Duty SelectedDuty
        {
            get
            {
                return _selectedDuty;
            }
            set
            {
                _selectedDuty = value;
                NotifyPropertyChanged("SelectedDuty");
            }
        }

        #endregion

        #region IAddUserPrentationModel Members

        private ContentType _extendInfo;
        public ContentType ExtendInfo
        {
            get
            {
                return _extendInfo;

            }
            set
            {
                _extendInfo = value;
                NotifyPropertyChanged("ExtendInfo");
            }
        }

        #endregion

        #region IAddUserPrentationModel 成员

        private ObservableCollection<Human> _allHuman;
        public ObservableCollection<Human> AllHuman
        {
            get
            {
                return _allHuman;
            }
            set
            {
                _allHuman = value;
                NotifyPropertyChanged("AllHuman");
            }
        }

        #endregion

        #region IAddUserPrentationModel 成员


        public void ShowDetial(PrivegeTypes privegeTypes,string DataKey)
        {
            DetailPrivege = privegeTypes;
            switch (privegeTypes)
            {
                case PrivegeTypes.New:
                    CW_User cw = new CW_User(this);
                    cw.Show();
                    break;
                default:
                    _domaincontext = new ELMServiceClient();
                    _domaincontext.GetT_HummanByUidAsync(DataKey);
                    _domaincontext.GetT_HummanByUidCompleted += (sender, data) =>
                    {
                        if (data.Error == null)
                        {
                            AddingHuman = data.Result;
                            SelectedDuty = AllDuty.Single(c => c.DutyId == AddingHuman.DutyId);
                             cw = new CW_User(this);
                            cw.Show();
                        }
                        else
                        {
                            throw data.Error;
                        }
                    };
                    break;
            }
           

            
        }

        #endregion

        #region IAddUserPrentationModel 成员

        private PrivegeTypes _detailPrivege;
        public PrivegeTypes DetailPrivege
        {
            get
            {
                return _detailPrivege;
            }
            set
            {
                _detailPrivege = value;
                NotifyPropertyChanged("DetailPrivege");
            }
        }

        #endregion

        #region IAddUserPrentationModel 成员


      
        public void SetAddingHuman(string Hmid)
        {
            
             
           
        }

        #endregion

        #region IAddUserPrentationModel 成员


        public void UpdateUser()
        {
            
            BusyMessage.Show();
            _domaincontext = new ELMServiceClient();
            _domaincontext.UpdateHumanAsync(AddingHuman);
            _domaincontext.UpdateHumanCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        initialData();
                        BusyMessage.Close();
                    }
                };
        }

        #endregion

        #region IAddUserPrentationModel 成员


        public void delUser(Human hm)
        {
            BusyMessage.Show();
            _domaincontext = new ELMServiceClient();
            _domaincontext.DelHumanAsync(hm);
            _domaincontext.DelHumanCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        BusyMessage.Close();
                        initialData();
                    }
                };
        }

        #endregion

        #region IAddUserPrentationModel Members


        public void delUser(string hmid)
        {
            BusyMessage.Show();
            _domaincontext = new ELMServiceClient();
            _domaincontext.DelHumanByIdAsync(hmid);
            _domaincontext.DelHumanByIdCompleted += (sender, data) =>
            {
                if (data.Error == null)
                {
                    BusyMessage.Close();
                    initialData();
                }
            };
        }

        #endregion

        #region IAddUserPrentationModel Members


        public ELMListUC ELMListData
        {
            get;
            set;
        }

        #endregion
    }
}
